package czh.cocoa.ui;

import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSet;

public class UIResponder extends NSObject {
	
	public boolean becomeFirstResponder() {
		if (canBecomeFirstResponder()) { 
			CocoaView root = (CocoaView) UIApplication.sharedApplication().getRootView();
			root.setFirstResponder(this);
			return true;
		}
		return false;
	}
	
	public boolean canBecomeFirstResponder() {
		return false;
	}
	
	public boolean canResignFirstResponder() {
		return true;
	}
	
	public boolean isFirstResponder() {
		CocoaView root = (CocoaView) UIApplication.sharedApplication().getRootView();
		return root.firstResponder() == this;
	}
	
	public boolean resignFirstResponder() {
		if (canResignFirstResponder()) { 
			CocoaView root = (CocoaView) UIApplication.sharedApplication().getRootView();
			if (root.firstResponder() == this) {
				root.setFirstResponder(null); 
			} 
			return true;
		}
		return false;
	}
	
	public UIResponder nextResponder() {
		return null;
	}

	public void touchesBeganWithEvent(NSSet touches, UIEvent event) {
		UIResponder responder = nextResponder();
		if (responder != null) {
			responder.touchesBeganWithEvent(touches, event);
		}
	}
	
	public void touchesMovedWithEvent(NSSet touches, UIEvent event) {
		UIResponder responder = nextResponder();
		if (responder != null) {
			responder.touchesMovedWithEvent(touches, event);
		}
	}
	
	public void touchesEndedWithEvent(NSSet touches, UIEvent event) {
		UIResponder responder = nextResponder();
		if (responder != null) {
			responder.touchesEndedWithEvent(touches, event);
		}
	}
	
	public void touchesCancelledWithEvent(NSSet touches, UIEvent event) {
		UIResponder responder = nextResponder();
		if (responder != null) {
			responder.touchesCancelledWithEvent(touches, event);
		}
	}
	
}
